package com.shanlitech.et.utils;

import com.google.common.primitives.Bytes;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class FastUtils {

    public static String conf_filename = ConfigUtil.get("conf_filename");
    public static String nginx_url = ConfigUtil.get("nginx_url");

    public static byte[] download(String url) throws Exception{

        ClientGlobal.init(conf_filename);

        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getConnection();
        StorageServer storageServer = null;

        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        // group1__M00/00/00/wKgBDFvBnviALiwMAAAAAgAAAAA007.txt
        String[] urls = url.split("__");
        if(urls.length != 2) return null;

        byte[] b = storageClient.download_file(urls[0], urls[1]);
        return b;

    }

    public static String upload(byte[] b)  throws Exception{
        ClientGlobal.init(conf_filename);

        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getConnection();
        StorageServer storageServer = null;

        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//          NameValuePair nvp = new NameValuePair("age", "18");
        String fileIds[] = storageClient.upload_file(b, "hex", null);
        if(fileIds.length != 2) return null;
        return fileIds[0] + "/" + fileIds[1];
    }

}
